package net.lwf.web.tags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

import net.lwf.service.AppHitCounter;

public class HitCounterTag extends TagSupport {
	
	private static final long serialVersionUID = 1L;
	//private static final String HIT_VALUE = "hitvalue";

	public int doStartTag() throws JspException {
		//HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		try {
			long hitcounter = AppHitCounter.getCounter();
			pageContext.getOut().print(String.valueOf(hitcounter));
		} catch (Exception ex) {
			throw new JspTagException("SimpleTag: " + ex.getMessage());
		}
		return SKIP_BODY;
	}

	public int doEndTag() {
		return EVAL_PAGE;
	}
	
/*	private String getHitValue(HttpServletRequest request) {
		long counter = 0;
		WebUser user = (WebUser) WebUtil.getAttribute(request, IWebConstant.WEB_USER);
		if(user == null) {
			counter = AppHitCounter.getInstance().getCounter();
		}
		else {
			//User logged
		}
		
		long value = 0;
		HttpSession session = request.getSession();
		Object hitvalue = session.getAttribute(HIT_VALUE);
		if(hitvalue == null) {
			//value = AppHitCounter.getInstance().getCounter(true);
			//value = new AppHitCounter().getCounter();
			session.setAttribute(HIT_VALUE, new Long(value));
		}
		else {
			value = ((Long) hitvalue).longValue();
		}

		return String.valueOf(value);
	}*/
	
	
/*	private static final long serialVersionUID = 1L;
	private static final String COUNTER_HOLDER = "/eclipse/workspace/protos/loginDemo/counter/counter.txt";
	private static final String HIT_VALUE = "hitvalue";

	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		try {
			pageContext.getOut().print(this.getHitValue(request));
		} catch (Exception ex) {
			throw new JspTagException("SimpleTag: " + ex.getMessage());
		}
		return SKIP_BODY;
	}

	public int doEndTag() {
		return EVAL_PAGE;
	}
	

	private void updateCount(long currentValue) {
		File file = new File(COUNTER_HOLDER);
		try {
			FileWriter filewriter = new FileWriter(file);
			PrintWriter printwriter = new PrintWriter(filewriter);
			printwriter.println(String.valueOf(currentValue));
			printwriter.close();
			filewriter.close();
		} catch (Exception exception1) {

		}
	}
	
	private synchronized String getHitValue(HttpServletRequest request) {
		long value = 0;
		HttpSession session = request.getSession();
		Object hitvalue = session.getAttribute(HIT_VALUE);
		if(hitvalue == null) {
			File file = new File(COUNTER_HOLDER);
			try {
				BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
				String hit_value = bufferedreader.readLine();
				bufferedreader.close();
				value = Long.parseLong(hit_value) + 1;
				this.updateCount(value);
				request.getSession().setAttribute(HIT_VALUE, new Long(value));
			} catch (Exception exception) {

			}
		}
		else {
			value = Long.parseLong(hitvalue.toString());
		}

		return String.valueOf(value);
	}*/
}
